Macaulay2 » Documentation
Packages » Python » PythonObject » PythonObject ^ PythonObject » PythonObject ^= Thing
next | previous | forward | backward | up | index | toc

PythonObject ^= Thing -- augmented exponentiation for Python objects

Description

Raise one Python object to the power of another, and assign the result to the first argument.

i1 : x = toPython 2

o1 = 2

o1 : PythonObject of class int
i2 : x ^= toPython 3

o2 = 8

o2 : PythonObject of class int
i3 : x

o3 = 8

o3 : PythonObject of class int

In addition to the Macaulay2-style ^=, this operation is also available using the Python-style **=.

i4 : x = toPython 6

o4 = 6

o4 : PythonObject of class int
i5 : x **= toPython 2

o5 = 36

o5 : PythonObject of class int
i6 : x

o6 = 36

o6 : PythonObject of class int

If the right-hand side is a Macaulay2 object, then it is first converted to a Python object before exponentiating.

i7 : x = toPython 16

o7 = 16

o7 : PythonObject of class int
i8 : x ^= 2

o8 = 256

o8 : PythonObject of class int
i9 : x

o9 = 256

o9 : PythonObject of class int

If the Python class of x defines an __ipow__ method for in-place exponentiation, then it will be called. Otherwise, x will be raised to the y power in the usual way, creating a new Python object that is assigned back to x.

For example, NumPy arrays support in-place exponentiation. In the example below, x is modified directly, and no new object is created.

i7 : installNumPyMethods();
i8 : x = toPython matrix {{1, 2}, {3, 4}}

o8 =  [[1 2]
       [3 4]]

o8 : PythonObject of class numpy.ndarray
i9 : x ^= 2

o9 = [[ 1  4]
      [ 9 16]]

o9 : PythonObject of class numpy.ndarray
i10 : x

o10 = [[ 1  4]
       [ 9 16]]

o10 : PythonObject of class numpy.ndarray

See also

Ways to use this method:


The source of this document is in Python/doc/arithmetic.m2:849:0.